package maneged;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import negocio.erro.GeralException;
import fachada.Fachada;
import basica.academico.Professor;
import basica.administrativo.Cargo;
import basica.administrativo.EstadoCivil;

@ManagedBean
public class MbProfessor {
	private Professor professor;
	private List<Cargo> listaCargos;
	public MbProfessor() {

		this.professor = new Professor();
		this.listaCargos = new ArrayList<Cargo>();
	}

	public Professor getProfessor() {
		return professor;
	}

	public void setProfessor(Professor professor) {
		this.professor = professor;
	}

	public List<Cargo> getListaCargos() {
		return listaCargos;
	}

	public void setListaCargos(List<Cargo> listaCargos) {
		this.listaCargos = listaCargos;
	}
	
	public EstadoCivil[] getEstadoCivil(){
		return EstadoCivil.values();		
	}

	public String salvarProfessor() throws GeralException {
		String url = new String();
		try {
			if (professor.getCodigo() == null || professor.getCodigo() == 0) {
				professor.setCodigo(null);
				Fachada.getInstancia().salvarProfessor(professor);
				this.msnSucesso();
				url = "professor-listar.xhtml";
			} else {
				Fachada.getInstancia().alterProfessor(professor);
				url = "professor-listar.xhtml";
			}
		} catch (Exception e) {
			this.msnErro();
			url = "professor.xhtml";
		}
		return url;
	}

	public List<Cargo> getListarCargos() {
		return Fachada.getInstancia().listarCargo();
	}

	// MENSSAGE
		// ============================================================================
		public void msnSucesso() {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO,
							"Professor salvo com sucesso!", ""));
		}
		
		public void removeSucesso() {
			FacesContext.getCurrentInstance().addMessage(
					null,
					new FacesMessage(FacesMessage.SEVERITY_INFO,
							"Professor removido com sucesso!", ""));
		}
	
	public void addWarn(ActionEvent actionEvent) {
		FacesContext.getCurrentInstance().addMessage(
				null,
				new FacesMessage(FacesMessage.SEVERITY_WARN,
						"Sample warn message", "Watch out for PrimeFaces!"));
	}

	public void addError(ActionEvent actionEvent) {
		FacesContext.getCurrentInstance()
				.addMessage(
						null,
						new FacesMessage(FacesMessage.SEVERITY_ERROR,
								"Sample error message",
								"PrimeFaces makes no mistakes"));
	}
	
	public void msnErro() {
		FacesContext.getCurrentInstance()
				.addMessage(
						null,
						new FacesMessage(FacesMessage.SEVERITY_ERROR,
								"Erro no cadastro do Professor",
								""));
	}

	public void addFatal(ActionEvent actionEvent) {
		FacesContext.getCurrentInstance().addMessage(
				null,
				new FacesMessage(FacesMessage.SEVERITY_FATAL,
						"Sample fatal message", "Fatal Error in System"));
	}
	
	public void msnErroFatal() {
		FacesContext.getCurrentInstance().addMessage(
				null,
				new FacesMessage(FacesMessage.SEVERITY_FATAL,
						"ERRO FATAL NO SISTEMA", "Fatal Error in System"));
	}
}
